package web.udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

/**
 * @description 接收
 * @className UdpReceiveDemo
 * @package web.udp
 * @author dongww@hsyuntai.com
 * @date 2020/3/10 16:39
 * @copyright 版权归 HSYUNTAI 所有
 */
public class UdpReceiveDemo {

    public static void main(String[] args) throws SocketException, IOException {

        //创建Socket 指定端口号
        DatagramSocket socket = new DatagramSocket(456);

        byte[] b = new byte[1024];
        DatagramPacket packet = new DatagramPacket(b, b.length);

        //阻塞
        socket.receive(packet);

        System.out.println(packet.getAddress());
        System.out.println(packet.getSocketAddress());

        System.out.println(packet.getPort());
        System.out.println(packet.getLength());

        System.out.println(packet.getAddress().getHostAddress());
        System.out.println(new String(packet.getData()));

        socket.close();

    }
}